package customerEnter.Frame.JDBCDao;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.FrameBeanSuper;
import customerEnter.Frame.DaoDataTransmission.PagingParametersResult;
import customerEnter.Frame.DaoDataTransmission.WebAppsInfo;
import customerEnter.Frame.Enum.FrameDataQueryUse;
import customerEnter.Frame.JDBCDao.daoFrame.SqlDataBaseController;
import customerEnter.Frame.JDBCDao.util.DaoFrameUtil;
import customerEnter.tool.WhetherToEmpty;

/**
 * 數據層的安全鍋爐設計 數據查詢方法做了更詳細的設計 每一結果對應一個方法
 * 
 * 类名称：Base 类描述： 创建人：dy 创建时间：2016-3-16 下午1:36:48 T 实体 k 数据传输类型(返回的類型)
 */

@Transactional
@SuppressWarnings("unchecked")
public class DaoFrameDMLBase<T, K extends FrameBeanSuper> extends DaoFrame<T, K> implements Serializable {
	/**
	 * serialVersionUID:TODO（用一句话描述这个变量表示什么）
	 * 
	 * @since Ver 1.1
	 */
	private static final long	serialVersionUID	= 5236434701172896178L;

	/**
	 * 查找這個表裡面所有數據返回 bean的所有屬性 有缓存
	 */
	protected List<?> findAll() {

		return (List<?>) findConditionQueryTransmission(null, null, null, null, false, true, FrameDataQueryUse.list)
				.getpObject();
	}

	/**
	 * 
	 * 根據id查找單個值 有缓存
	 */
	protected K findId(Integer id) {
		List<?> list = (List<?>) findConditionQueryTransmission(null, FindTClassName(), " where id=" + id, null, false,
				true, FrameDataQueryUse.list).getpObject();
		if (list.size() > 0) {
			return (K) list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 根據條件查找 有缓存
	 */
	protected List<K> findSeleList(String where) {
		return (List<K>) findConditionQueryTransmission(null, FindTClassName(),
				SqlDataBaseController.getWhere() + where, null, false, true, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找（填寫表名） 无缓存
	 */
	@SuppressWarnings("hiding")
	protected <K> List<K> findSeleList(String Front, String ClassName, String where) {
		return (List<K>) findConditionQueryTransmission(Front, ClassName, SqlDataBaseController.getWhere() + where,
				null, false, true, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找數據數量 无缓存
	 */
	protected List<K> findSeleCont(String Front, String ClassName, String where) {
		return (List<K>) findConditionQueryTransmission(Front, ClassName, SqlDataBaseController.getWhere() + where,
				null, false, true, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找（可查找數據傳輸類型 單次查詢 有緩存） //要記得在條件 參數 添加and。。
	 */
	public <A> List<A> findSelecClasses(String Front, String ClassName, String where, Class<?> Class) {

		return (List<A>) findConditionQueryTransmission(Front, ClassName, SqlDataBaseController.getWhere() + where,
				Class, false, true, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找（可查找數據傳輸類型 可多重查詢 有緩存） 沒有where 1=1
	 */
	@SuppressWarnings("hiding")
	protected <T> List<T> findSelecJoinOnTable(String Front, String ClassName, String where, Class<?> Class) {
		return (List<T>) findConditionQueryTransmission(Front, ClassName, where, Class, false, true,
				FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找（可查找數據傳輸類型 單次查詢 無緩存）
	 */
	protected List<?> findSelecNoEachClasses(String Front, String ClassName, String where, Class<?> Class) {
		return (List<?>) findConditionQueryTransmission(Front, ClassName, SqlDataBaseController.getWhere() + where,
				Class, false, false, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找（可查找數據傳輸類型 可多重查詢 無緩存）
	 */
	@Deprecated
	protected List<?> findSelecNoEachClassesS(String Front, String ClassName, String where, Class<?> Class) {
		return (List<?>) findConditionQueryTransmission(Front, ClassName, SqlDataBaseController.getWhere() + where,
				Class, false, false, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 根據條件查找（可查找數據傳輸類型 單次查詢 無緩存 返回單個對象）
	 */
	protected K findSelecNoEachSingle(String Front, String ClassName, String where) {

		List<?> list = (List<?>) findConditionQueryTransmission(Front, ClassName, where, null, false, false,
				FrameDataQueryUse.list).getpObject();
		if (WhetherToEmpty.ListWhetherToEmpty(list)) {
			return null;
		} else {
			return (K) list.get(0);
		}
	}

	/**
	 * 根據條件查找（可查找數據傳輸類型 單次查詢 無緩存 返回單個對象）
	 * 
	 * @param <OBj>
	 *            傳入的類型參數
	 */
	protected <Other> Other findSelecNoEachSingle(String Front, String ClassName, String where, Class<Other> Class) {
		List<?> list = (List<?>) findConditionQueryTransmission(Front, ClassName,
				SqlDataBaseController.getWhere() + where, Class, false, false, FrameDataQueryUse.list).getpObject();
		if (list.size() <= 0) {
			return null;
		} else {
			return (Other) list.get(0);
		}
	}

	/**
	 * 查詢數據 沒有緩存
	 */
	protected <Transmission> List<Transmission> findTransmissionData(String Front, String ClassName, String where,
			Class<?> Class) {

		return (List<Transmission>) findConditionQueryTransmission(Front, ClassName,
				SqlDataBaseController.getWhere() + where, Class, false, false, FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 查詢統計數據
	 */
	protected Integer findSingleData(String Front, String ClassName, String where) {
		return QueryCount(SqlAcquire(SqlDataBaseController.getDataBase(), SqlDataBaseController.getDMLSele(), Front,
				SqlDataBaseController.getRule(), ClassName, SqlDataBaseController.getWhere() + " " + where));
	}

	/**
	 * 添加 數據庫字段從左到右有序排列 LinkedList<Object> values 表示裡面的值 逐一添加
	 */
	protected String InsterTable(String TableName, LinkedList<Object> values) {
		return InsterObject(SqlDataBaseController.getDataBase(), TableName, values);
	}

	/**
	 * 添加 映射表 根據對象的屬性名和數據庫裡面的字段想對應
	 */
	public String saveTable(String Table, Object MappingObject) {
		return InsertMapping(SqlDataBaseController.getDataBase(), Table, MappingObject);
	}

	/**
	 * 修改數據庫 映射對象到數據庫
	 */
	public String UpdateTable(String TableName, Object MappingObject, String where) {
		return UpdateMappingObject(SqlDataBaseController.getDataBase(), TableName, MappingObject, where);
	}

	/**
	 * 使用sql語句 修改
	 */
	protected Integer UpdateSqlTable(String sql) {
		return UpdateObject(sql);
	}

	/**
	 * 分頁查詢數據 有緩存 自动排序 只需要写排序的字段名
	 */
	protected <Transmission> List<Transmission> findPagingTransmissionCacheData(String Front, String ClassName,
			String where, int size, int page, String order, String descOrAsc, Class<?> Class) {

		DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission = new DaoFrameDMLBaseTransmission();
		daoFrameDMLBaseTransmission.setFront(Front);
		daoFrameDMLBaseTransmission.setTableName(ClassName);
		daoFrameDMLBaseTransmission.setClassName(Class);
		QueryPage(Front, ClassName, where, size, page, order, descOrAsc, daoFrameDMLBaseTransmission);

		return (List<Transmission>) findConditionQueryTransmission(daoFrameDMLBaseTransmission.getFront(),
				daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getWhere(), Class, true, true,
				FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 分頁查詢數據 有緩存,自动排序 只需要写排序的字段名
	 */
	@SuppressWarnings("rawtypes")
	protected PagingParametersResult findPagingParametersResultCacheData(String Front, String ClassName, String where,
			int size, int page, String order, String descOrAsc, Class<?> Class) {
		DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission = new DaoFrameDMLBaseTransmission();
		daoFrameDMLBaseTransmission.setFront(Front);
		daoFrameDMLBaseTransmission.setTableName(ClassName);
		daoFrameDMLBaseTransmission.setClassName(Class);
		QueryPage(Front, ClassName, where, size, page, order, descOrAsc, daoFrameDMLBaseTransmission);
		return findConditionQueryTransmission(daoFrameDMLBaseTransmission.getFront(),
				daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getWhere(), Class, true, true,
				FrameDataQueryUse.list);
	}

	/**
	 * 分頁查詢數據 沒有緩存 自动排序 只需要写排序的字段名
	 */
	protected <Transmission> List<Transmission> findPagingTransmissionData(String Front, String ClassName,
			String where, int size, int page, String order, String descOrAsc, Class<?> Class) {

		DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission = new DaoFrameDMLBaseTransmission();
		daoFrameDMLBaseTransmission.setFront(Front);
		daoFrameDMLBaseTransmission.setTableName(ClassName);
		daoFrameDMLBaseTransmission.setClassName(Class);
		QueryPage(Front, ClassName, where, size, page, order, descOrAsc, daoFrameDMLBaseTransmission);
		return (List<Transmission>) findConditionQueryTransmission(daoFrameDMLBaseTransmission.getFront(),
				daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getWhere(), Class, true, false,
				FrameDataQueryUse.list).getpObject();
	}

	/**
	 * 分頁查詢數據 沒有緩存 自动排序 只需要写排序的字段名
	 */
	@SuppressWarnings("rawtypes")
	protected PagingParametersResult findPagingParametersResultData(String Front, String ClassName, String where,
			int size, int page, String order, String descOrAsc, Class<?> Class) {

		DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission = new DaoFrameDMLBaseTransmission();

		daoFrameDMLBaseTransmission.setFront(Front);

		daoFrameDMLBaseTransmission.setTableName(ClassName);

		daoFrameDMLBaseTransmission.setClassName(Class);

		QueryPage(Front, ClassName, where, size, page, order, descOrAsc, daoFrameDMLBaseTransmission);

		return findConditionQueryTransmission(daoFrameDMLBaseTransmission.getFront(),
				daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getWhere(), Class, true, false,
				FrameDataQueryUse.list);
	}

	/**
	 * 根據id刪除數據
	 */
	public Integer deleteObject(String TableName, int id) {

		if ("MySQL Connector Java".contains(WebAppsInfo.webDataBase)) {
			return deleteObject(SqlDataBaseController.getDataBase() + " delete from " + TableName + " where id=" + id);
		} else if ("Microsoft SQL Server JDBC Driver 3.0".equals(WebAppsInfo.webDataBase)) {
			return deleteObject(SqlDataBaseController.getDataBase() + " delete " + TableName
					+ SqlDataBaseController.getWhere() + " And id=" + id);
		} else {
			return 0;
		}
	}

	/**
	 * 根據id刪除數據 批量
	 */
	public Integer deleteObject(String TableName, String where) {
		if ("MySQL Connector Java".contains(WebAppsInfo.webDataBase)) {
			return deleteObject(SqlDataBaseController.getDataBase() + " delete from " + TableName
					+ SqlDataBaseController.getWhere() + "   " + where);
		} else if ("Microsoft SQL Server JDBC Driver 3.0".equals(WebAppsInfo.webDataBase)) {
			return deleteObject(SqlDataBaseController.getDataBase() + " delete " + TableName
					+ SqlDataBaseController.getWhere() + where);
		} else {
			return 0;
		}
	}

	/**
	 * @user 力力 批量刪除---condition->條件，str->範圍 （如str = "1,2,3,4")，以逗號分隔
	 */
	public Integer deleteBatch(String TableName, String condition, String str) {
		if ("MySQL Connector Java".contains(WebAppsInfo.webDataBase)) {
			return deleteObject(SqlDataBaseController.getDataBase() + " delete from " + TableName + " where  "
					+ condition + " in (" + str + ")");
		} else if ("Microsoft SQL Server JDBC Driver 3.0".equals(WebAppsInfo.webDataBase)) {
			return deleteObject(SqlDataBaseController.getDataBase() + " delete " + TableName + " where  " + condition
					+ " in (" + str + ")");
		} else {
			return 0;
		}
	}

	/**
	 * 批量增加數據
	 * 
	 * @param <Transmission>
	 */

	public <Transmission> Integer InsertBatch(String TableName, List<Transmission> t, Class<?> class1) {
		Integer integer = 0;
		try {
			integer = InsertBatchData(SqlDataBaseController.getDataBase(), TableName, t, class1);
		} catch (Exception e) {
			customerEnter.Frame.Utils.Log4j.Log4jOutInfo(e.getMessage());
		}
		return integer;
	}

	/**
	 * 初始化 类型参数 创建一个新的实例 DMLBase.
	 * 
	 */
	public DaoFrameDMLBase() {

		// TODO Auto-generated constructor stub
		System.out.println("初始化:" + getClass());
	}

	@SuppressWarnings("rawtypes")
	public <DaoAutomatic> DaoAutomatic queryDMLId(int id, Class<DaoAutomatic> Class) {
		PagingParametersResult pagingParametersResult = findConditionQueryTransmission(null, FindTClassName(Class),
				SqlDataBaseController.getWhere() + " and id=" + id, Class, false, true, FrameDataQueryUse.list);
		List<?> list = (List<?>) pagingParametersResult.getpObject();
		if (list.size() > 0) {
			return (DaoAutomatic) list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 返回的數據是單行
	 * 
	 * map string string[] int int[]
	 */
	public <DaoAutomatic> DaoAutomatic querySingleDate(String Front, String ClassName, String where, Class<?> Class) {

		return (DaoAutomatic) findConditionQueryTransmission(Front, ClassName, where, Class, false, false,
				DaoFrameUtil.queryClassIsUse(Class)).getpObject();
	}
}
